package com.health.mapp.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.PatientShortInfo;
import com.health.db.model.RegisterSchedule;
import com.health.db.model.StuffList;
import com.health.db.service.order.IRegisterScheduleService;
import com.health.db.service.user.IPatientShortInfoService;
import com.health.db.service.user.IStuffListService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.UserRegisterDoctorReq;
import com.health.mapp.bean.UserRegisterDoctorResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 医生挂号
 * @author yuchong
 */
public class UserRegisterDoctorImpl20180715 implements ActionInterface{

	@Autowired
	IStuffListService stuffListService;
	@Autowired
	IPatientShortInfoService  patientShortInfoService;
	@Autowired
	IRegisterScheduleService registerScheduleService;
	 
    private static Logger logger = Logger.getLogger(UserRegisterDoctorImpl20180715.class);

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {


    	UserRegisterDoctorReq req = (UserRegisterDoctorReq) reqBean;
    	UserRegisterDoctorResp _resp = new UserRegisterDoctorResp();
    	
    	String userId= req.getUserId();
    	String stuffId = req.getStuffId();
    	String patientId=req.getPatientId();
    	String registerDate = req.getRegisterDate();
    	
    	RegisterSchedule registerSchedule = new RegisterSchedule();
    	registerSchedule.setUserId(Long.valueOf(userId));
    	registerSchedule.setStuffId(Long.valueOf(stuffId));
    	registerSchedule.setPatientId(Long.valueOf(patientId));
    	registerSchedule.setStatus((short) 0);
    	if (StringUtils.isNotEmpty(registerDate)) {
    		registerSchedule.setScheduleDate(registerDate);
		}else{
			registerSchedule.setScheduleDate(DateUtils.currentDate());
		}
    	
    	if (StringUtils.isNotEmpty(userId)&&StringUtils.isNotEmpty(stuffId)&&StringUtils.isNotEmpty(patientId)) {
    		
    		//查询医生基础信息
    		StuffList stuff = stuffListService.selectByStuffId(Long.valueOf(stuffId));
    		if(null==stuff){
    			_resp.setErrorCode(-1);
   	            _resp.setErrorMsg("医生查询失败");
   	            return _resp;
    		}else{
    			
    			registerSchedule.setRegisterPrice(stuff.getRegisterPrice());
    			registerSchedule.setStuffName(stuff.getStuffName());
    			registerSchedule.setDepartmentId(stuff.getDepartmentId());
    			registerSchedule.setDepartmentName(stuff.getDepartmentName());
    		}
    		//查询宝宝信息
    		PatientShortInfo patientShortInfo = patientShortInfoService.selectById(Long.valueOf(patientId));
    		if(null==patientShortInfo){
    			_resp.setErrorCode(-1);
   	            _resp.setErrorMsg("挂号信息查询失败");
   	            return _resp;
    		}else{
    			registerSchedule.setPatientName(patientShortInfo.getName());
    			registerSchedule.setMedicalCardNo(patientShortInfo.getMedicalCardNo());
    		}
    		registerScheduleService.insert(registerSchedule);
    		
    		_resp.setRegister(registerSchedule);
    		
		}else{
			 _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
	         _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
	         return _resp;
		}
    	
		 _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		 _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
        return _resp;
    }

	public IStuffListService getStuffListService() {
		return stuffListService;
	}

	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}

	public IPatientShortInfoService getPatientShortInfoService() {
		return patientShortInfoService;
	}

	public void setPatientShortInfoService(
			IPatientShortInfoService patientShortInfoService) {
		this.patientShortInfoService = patientShortInfoService;
	}

	public IRegisterScheduleService getRegisterScheduleService() {
		return registerScheduleService;
	}

	public void setRegisterScheduleService(
			IRegisterScheduleService registerScheduleService) {
		this.registerScheduleService = registerScheduleService;
	}
    
}
